package com.socket;

import com.alibaba.fastjson2.JSONObject;
import com.mybatisdemo.service.IotMessageService;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 功能描述:
 *
 * @author wuyanjun
 * @version V1.0
 * @create 2025/6/18
 */
//@Component
public class MessageTask {

    @Autowired
    private WebSocketServer webSocketServer;

    private final ScheduledExecutorService service= Executors.newSingleThreadScheduledExecutor();

    @PostConstruct
    public void init() throws IOException {
        service.schedule(() -> {
            try {
                webSocketServer.sendMessageToAll("项目启动10秒后只执行一次");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        },10,TimeUnit.SECONDS);

    }


//    @Scheduled(fixedDelayString = "10000")
//    public void sendMessageToAllClient() throws IOException {
//        webSocketServer.sendMessageToAll("项目启动10秒后执行，每10秒执行一次");
//    }

}
